package geguol.cc.widgetapplication.activity;

import android.content.res.AssetManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import geguol.cc.widgetapplication.R;
import geguol.cc.widgetapplication.utils.ParseConfigurationFileException;

import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="MainActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//这个 控件就是多选的意思啊

		MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.mult_com);

		ArrayAdapter<String> adapter = new ArrayAdapter(this,
	            android.R.layout.simple_dropdown_item_1line,
				getResources().getStringArray(R.array.country));
		textView.setAdapter(adapter);
		textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
	}

	public void showLocal(View view) throws IOException {
		String path = getExternalFilesDir("") + "/databases/";
//		String path = Environment.getExternalStorageDirectory().getPath()+ "/databases/";
//		Toast.makeText(MainActivity.this,path, Toast.LENGTH_SHORT).show();
		Log.d(TAG, "showLocal: "+path);
		File dir = new File(path);
		if (!dir.exists()){
			dir.mkdirs();
		}
		File file = new File(path,"hello.txt");
		FileOutputStream os = new FileOutputStream(file);
		String text = "天下熙熙皆为利来,天下攘攘皆为利往";
		os.write(text.getBytes());
		os.close();
	}

	public void showAssert(View v) throws IOException {
		getConfigInputStream();
	}

	// file:///android_asset/paper.html
	private void getConfigInputStream() throws IOException {
		AssetManager assetManager = getAssets();
		String[] fileNames = assetManager.list("");
		if (fileNames != null && fileNames.length > 0) {
			for (String fileName : fileNames) {
				if ("paper.html".equalsIgnoreCase(fileName)) {
//					InputStream is = assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
				}
				Log.d(TAG, "getConfigInputStream: "+fileName);
			}
		}
//		throw new ParseConfigurationFileException();

	/*	File file = new File("file:///android_assets/paper.html");
		if (file.exists()){
			Log.d(TAG, "getConfigInputStream: "+file.getAbsolutePath());
		}else {
			throw new ParseConfigurationFileException();
		}*/
	}
}
